#
#

import os, sys
if __name__ == '__main__':
	execfile(os.path.join(sys.path[0], 'framework.py'))

from Testing import ZopeTestCase
from Products.CMFPlone.tests import PloneTestCase
ZopeTestCase.installProduct('Archetypes')
ZopeTestCase.installProduct('MimetypesRegistry')
ZopeTestCase.installProduct('PortalTransforms')
ZopeTestCase.installProduct('ArchSurvey')

class SurveyTestCase(PloneTestCase.PloneTestCase):
	"""Superclass for TestSurvey and TestQuestions"""
	
	def afterSetUp(self):
		self.portal.portal_quickinstaller.installProduct('ArchSurvey')
		self.portal.portal_quickinstaller.installProduct('ArchSurvey')	
		self.portal.acl_users._doAddUser('manager', 'secret', ['Manager'], [])
	
	def addSurvey(self):
		self.loginPortalOwner()
		self.portal.invokeFactory('Survey','s')
		return getattr(self.portal,'s')
	
	def addQuestion(self, type):
		s = self.addSurvey()
		s.addQuestion(type)
		return s.listQuestions()[0]	

class TestSurvey(SurveyTestCase):

	def testWorkflowInstalled(self):
		"""Test that we have the right workflow"""
		workflow = self.portal.portal_workflow.getChainFor('Survey')
		self.failUnless(workflow == ('survey_workflow',))
		
	def testAddSurvey(self):
		"""Test whether we can add a survey"""
		s = self.addSurvey()
		
	def testAddQuestion(self):
		"""Test if we can add questions"""
		s = self.addSurvey()
		s.addQuestion('ListQuestion',page='default')
		self.failUnless(len(s.listQuestions()) == 1)

	def testAddPage(self):
		"""Test if we can add pages"""
		s = self.addSurvey()
		orig = s.getPages()[0]
		s.addPage(2)
		self.failUnless(len(s.getPages()) == 2)
		self.failUnless(s.getPages()[0] == orig)
		self.failUnless(s.getPages()[1] != orig)
		
	def testDelPage(self):
		"""Test if we delete pages right"""
		s = self.addSurvey()
		s.addPage(2)
		(x,y) = s.getPages()
		s.addQuestion('ListQuestion',page=x)
		s.delPage(x)
		pages = s.getPages()
		qs = s.listQuestions()
		self.failUnless(pages == [y], "%s are pages" % pages )
		self.failUnless(qs == [], "%s are still questions" % qs)
		
		
def test_suite():
	from unittest import TestSuite, makeSuite
	suite = TestSuite()
	suite.addTest(makeSuite(TestSurvey))
	return suite

if __name__ == '__main__':
	framework()

	   
